package com.seis_645.appTesting;

import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class SalesPersonActivity extends ListActivity {

	private SalesPersonDataSource datasource;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.salesperson_main);
		final ListView lv = getListView();

		datasource = new SalesPersonDataSource(this);
		datasource.open();
		List<Salesperson> values = datasource.get_all_salespeople();
		ArrayAdapter<Salesperson> adapter = new ArrayAdapter<Salesperson>(this,
				android.R.layout.simple_list_item_1, values);
		setListAdapter(adapter);

		lv.setAdapter(adapter);

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> a, View v, int position,
					long id) {
				Salesperson person = (Salesperson) lv.getItemAtPosition(position);
				datasource.delete(person);
			}
		});
	}
	
	@Override
	protected void onDestroy() {
		datasource.close();
		super.onDestroy();
	}
	
	@Override
	protected void onPause() {
		datasource.close();
		super.onPause();
	}
	
	@Override 
	protected void onResume() {
		datasource.open();
		super.onResume();
	}
	
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_add_sales:
			Intent new_sales = new Intent(getParent(),
					SaveSalesPersonActivity.class);
			TabGroupActivity parentActivity = (TabGroupActivity) getParent();
			parentActivity.startChildActivity("SaveSalesPersonActivity", new_sales);
			break;
		}
		return;
	}

}
